﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Aliyeye.Car.Net.UI.Components;
using Aliyeye.Car.Net.UI.PrintExport;
using Aliyeye.Car.Net.UI.Common;
using Aliyeye.Car.Net.UI.PopupDialog;

namespace Aliyeye.Car.Net.UI.OrderModel
{
    /// <summary>
    /// 结算&续租
    /// </summary>
    public partial class GetCarList : Aliyeye.Car.Net.UI.BaseForm
    {
        private string strSearch = string.Empty;
        Model.Customer customerInfo = new Model.Customer();
        int customID = 0;
        string customerName = "";
        string customerCode = "";


        public GetCarList()
        {
            InitializeComponent();
        }

        private void ShortLeaseWithDriverList_Load(object sender, EventArgs e)
        {
            this.pager1.PageCurrent = 1;
            this.pager1.Bind();
        }

        private int dgvBind()
        {
            string _strSql = "OrderSataus='false' and IsGetCar !='true'";
            string _strSql2 = "";

            string strWhere = strSearch;
            PageData pageData = new PageData(this, pager1, overlay);
            pageData.TableName = "Reserve left join Car on Car.Id=Reserve.CarId and (Reserve.OrderSataus!='true'or Reserve.OrderSataus is null) " + " left join Customer on Customer.Id=Reserve.CustId";
            pageData.PrimaryKey = "Reserve.id";
            pageData.OrderStr = "Reserve.InpuTime desc";
            pageData.PageIndex = this.pager1.PageCurrent;
            pageData.PageSize = this.pager1.PageSize;
            pageData.QueryCondition = _strSql + _strSql2 + strWhere.ToString();
            pageData.QueryFieldName = "Reserve.Code,CustId,Customer.Code as CustomerCode,Customer.Name as CustomerName,BookTime,EndBooktime,PayType,Type,Car.Code as CarCode,Brand,Car.Cartype,CarID,Reserve.id as ReserveId,ZPrice,Deposit,Rent,DeadLineType,YearRent,MonthRent,AirportShuttleOneWay,AirportShuttleTwoWay,Chartered3Rent,OutdatedFee3,Chartered8Rent,OutdatedFee8,LongJourneys1,LongJourneys2,LongJourneys3,MonthDeposit,YearDeposit";

            DataSet ds = pageData.QueryDataTable();
            dgvInfo.AutoGenerateColumns = false;
            dgvInfo.DataSource = this.pager1.bindingSource;
            return pageData.TotalCount;
        }

        private int pager1_EventPaging(EventPagingArg e)
        {
            return dgvBind();
        }

        private void dgvInfo_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (IsANonHeaderButtonCell(e))
            {
                ButtonClicked(e);
            }
        }

        int carid = 0;
        int reserveId = 0;
        private void ButtonClicked(DataGridViewCellEventArgs e)
        {
            String strButtonContent = dgvInfo.Columns[e.ColumnIndex].Name;
            carid = Convert.ToInt32(dgvInfo.Rows[e.RowIndex].Cells["CarID1"].Value.ToString());
            if (dgvInfo.Rows[e.RowIndex].Cells["ReserveId1"].Value != null && dgvInfo.Rows[e.RowIndex].Cells["ReserveId1"].Value.ToString().Trim() != "")
                reserveId = Convert.ToInt32(dgvInfo.Rows[e.RowIndex].Cells["ReserveId1"].Value.ToString());

            string type = dgvInfo.Rows[e.RowIndex].Cells["租车方式"].Value.ToString();

            ////长租带驾

            String MonthRent = dgvInfo.Rows[e.RowIndex].Cells["每月租金"].Value.ToString();
            String YearRent = dgvInfo.Rows[e.RowIndex].Cells["每年租金"].Value.ToString();
            String MonthDeposit = dgvInfo.Rows[e.RowIndex].Cells["每月押金"].Value.ToString();
            String YearDeposit = dgvInfo.Rows[e.RowIndex].Cells["每年押金"].Value.ToString();

            ////短租带驾
            String AirportShuttleOneWay = dgvInfo.Rows[e.RowIndex].Cells["机场接送单程"].Value.ToString();
            String AirportShuttleTwoWay = dgvInfo.Rows[e.RowIndex].Cells["机场接送来回"].Value.ToString();
            String Chartered3Rent = dgvInfo.Rows[e.RowIndex].Cells["三时包车"].Value.ToString();
            String Chartered8Rent = dgvInfo.Rows[e.RowIndex].Cells["八时包车"].Value.ToString();
            String OutdatedFee3 = dgvInfo.Rows[e.RowIndex].Cells["过时费3"].Value.ToString();
            String OutdatedFee8 = dgvInfo.Rows[e.RowIndex].Cells["过时费8"].Value.ToString();
            String LongJourneys1 = dgvInfo.Rows[e.RowIndex].Cells["长途旅程一"].Value.ToString();
            String LongJourneys2 = dgvInfo.Rows[e.RowIndex].Cells["长途旅程二"].Value.ToString();
            String LongJourneys3 = dgvInfo.Rows[e.RowIndex].Cells["长途旅程三"].Value.ToString();

            //if (strButtonContent.Trim().Equals("取车"))
            //{
            //    panelInput.Visible = true;
            //}

            if (strButtonContent.Trim().Equals("取车"))
            {
                switch (type.Trim())
                {
                    case "长租自驾":
                    case "1":
                        ReserveShortWithOutDriver userForm3 = new ReserveShortWithOutDriver(false, carid, reserveId, MonthRent, YearRent, true);
                        userForm3.ShowDialog();
                        break;
                    case "短租自驾":
                    case "2":
                        ReserveShortWithOutDriver userForm4 = new ReserveShortWithOutDriver(true, carid, reserveId, MonthRent, YearRent, true);
                        userForm4.ShowDialog();
                        break;
                    case "长租带驾":
                    case "3":
                        ReserveLongWithDriver userForm = new ReserveLongWithDriver(reserveId, carid, MonthRent, YearRent, MonthDeposit, YearDeposit, true);
                        userForm.ShowDialog();
                        break;
                    case "短租带驾":
                    case "4":
                        ReserveShortWithDriver userForm1 = new ReserveShortWithDriver(reserveId, carid, AirportShuttleOneWay, AirportShuttleTwoWay,
                    Chartered3Rent, Chartered8Rent, OutdatedFee3, OutdatedFee8, LongJourneys1, LongJourneys2, LongJourneys3, true);
                        userForm1.ShowDialog();
                        break;
                }

                this.pager1.Bind();
            }

        }

        private bool IsANonHeaderButtonCell(DataGridViewCellEventArgs cellEvent)
        {
            if (dgvInfo.Columns[cellEvent.ColumnIndex] is
                 DataGridViewButtonColumn &&
                 cellEvent.RowIndex != -1)
            { return true; }
            else { return (false); }
        }

        //搜索
        private void btnSearch_Click(object sender, EventArgs e)
        {
            string strWhere = "";
            if (!string.IsNullOrEmpty(txtcustcode.Text))
            {
                strWhere += " And   Car.Code like '%" + txtcustcode.Text + "%'";
            }
            strSearch = strWhere;
            this.pager1.Bind();
        }

        //全部
        private void btnAll_Click(object sender, EventArgs e)
        {
            strSearch = "";
            txtcustcode.Text = "";
            txtName.Text = "";
            this.pager1.Bind();
        }

        private void dgvInfo_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (this.dgvInfo.Rows.Count != 0)
            {
                for (int i = 0; i < this.dgvInfo.Rows.Count; )
                {
                    this.dgvInfo.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.PapayaWhip;
                    i += 2;
                }
            }
        }

        private void dgvInfo_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            e.CellStyle.WrapMode = DataGridViewTriState.True;
            if (this.dgvInfo.Columns[e.ColumnIndex].Name == "租车方式")
            {
                if (e.Value != null)
                {
                    string type = this.dgvInfo.Rows[e.RowIndex].Cells["租车方式"].Value.ToString().Trim();
                    switch (type.Trim())
                    {
                        case "1":
                            e.Value = "长租自驾";
                            break;
                        case "2":
                            e.Value = "短租自驾";
                            break;
                        case "3":
                            e.Value = "长租带驾";
                            break;
                        case "4":
                            e.Value = "短租带驾";
                            break;
                    }
                }
            }
            if (this.dgvInfo.Columns[e.ColumnIndex].Name == "还车日期")
            {
                if (e.Value != null)
                {
                    string strCarTime = "";
                    strCarTime = Convert.ToDateTime(e.Value.ToString().Trim()).ToString("yyyy-MM-dd");
                    if (strCarTime == "1990-01-01")
                        e.Value = "";
                    else
                        e.Value = Convert.ToDateTime(e.Value.ToString().Trim()).ToString("yyyy-MM-dd HH:mm:ss");
                }
            }
            if (this.dgvInfo.Columns[e.ColumnIndex].Name == "预约日期")
            {
                if (e.Value != null)
                {
                    string strCarTime = "";
                    strCarTime = Convert.ToDateTime(e.Value.ToString().Trim()).ToString("yyyy-MM-dd");
                    if (strCarTime == "1990-01-01")
                        e.Value = "";
                    else
                        e.Value = Convert.ToDateTime(e.Value.ToString().Trim()).ToString("yyyy-MM-dd HH:mm:ss");
                }
            }
            if (this.dgvInfo.Columns[e.ColumnIndex].Name == "租赁单价")
            {
                if (e.Value != null)
                {
                    string DeadLineType = this.dgvInfo.Rows[e.RowIndex].Cells["DeadLineType"].Value.ToString().Trim();
                    e.Value = e.Value + "/" + DeadLineType;
                }
            }
        }

        private void txtcustcode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string strSearch = txtcustcode.Text;
                if (string.IsNullOrEmpty(strSearch))
                {
                    DialogWarning dia = new DialogWarning("客户编号不能为空！");
                    dia.ShowDialog();
                    return;
                }
                else
                {
                    DisplayCustomeLIst(strSearch, "客户编号");
                }
            }
        }

        private void txtName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string strSearch = txtName.Text;
                if (string.IsNullOrEmpty(strSearch))
                {
                    DialogWarning dia = new DialogWarning("客户名不能为空！");
                    dia.ShowDialog();
                    return;
                }
                else
                {
                    DisplayCustomeLIst(strSearch, "姓名");
                }
            }
        }
        private void DisplayCustomeLIst(string strSearch, string type)
        {
            if (string.IsNullOrEmpty(strSearch))
            {
                return;
            }
            string strSelectWhere = "";
            if (type == "姓名")
            {
                strSelectWhere = " Name like '%" + strSearch + "%'";
            }
            if (type == "客户编号")
            {
                strSelectWhere = " Code like '%" + strSearch + "%'";
            }
            DataSet ds = null;
            dgvCustomInfo.DataSource = null;
            dgvCustomInfo.AutoGenerateColumns = false;
            CustomerSvc.SoapHeaderClass sopaHeader = new Aliyeye.Car.Net.UI.CustomerSvc.SoapHeaderClass();
            ServiceManipulator.SafeAsyncExecution<CustomerSvc.CustomerServiceSoapClient>(
                proxy => proxy.GetList(sopaHeader, strSelectWhere),
                result =>
                {
                    if (result.Error == null)
                    {
                        ds = (DataSet)result.Result;
                        if (ds != null && ds.Tables[0].Rows.Count > 0)
                        {
                            dgvCustomInfo.DataSource = ds.Tables[0];
                            this.dgvCustomInfo.Visible = true;
                            this.dgvCustomInfo.Focus();
                        }
                        else
                        {
                            txtcustcode.Text = customerCode;
                            txtName.Text = customerName;
                            PageCommon.Alert("查找的客户不存在，请重新查找！");
                        }
                        overlay.FadeOut(); Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut(); Cursor.Current = Cursors.Default;
                    }
                },
                this
            );

        }

        private void dgvCustomInfo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                customerInfo.Id = Convert.ToInt32(this.dgvCustomInfo.CurrentRow.Cells["编号"].Value.ToString());
                customerInfo.Name = this.dgvCustomInfo.CurrentRow.Cells["姓名"].Value.ToString();
                customerInfo.Tel = this.dgvCustomInfo.CurrentRow.Cells["移动电话"].Value.ToString();
                customerInfo.ComName = this.dgvCustomInfo.CurrentRow.Cells["单位名称"].Value.ToString();

                this.txtcustcode.Text = this.dgvCustomInfo.CurrentRow.Cells["客户编号1"].Value.ToString();
                this.txtName.Text = this.dgvCustomInfo.CurrentRow.Cells["姓名"].Value.ToString();
                customID = Convert.ToInt32(this.dgvCustomInfo.CurrentRow.Cells["编号"].Value.ToString());
                customerName = this.txtName.Text;
                customerCode = this.txtcustcode.Text;
                this.dgvCustomInfo.Visible = false;
            }
        }

        private void dgvCustomInfo_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (this.dgvCustomInfo.Rows.Count != 0)
            {
                for (int i = 0; i < this.dgvCustomInfo.Rows.Count; )
                {
                    this.dgvCustomInfo.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.PapayaWhip;
                    i += 2;
                }
            }
        }
    }
}
